package com.shenhaofeng.demo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shenhaofeng.app.adapter.ModuleAdapter;
import com.shenhaofeng.app.adapter.SingleTypeMoreAdapter;
import com.shenhaofeng.app.adapter.Status;
import com.shenhaofeng.app.component.BaseActivity;
import com.shenhaofeng.app.loader.list.DataSource;
import com.shenhaofeng.app.loader.list.Loader;
import com.shenhaofeng.app.loader.list.listener.OnMoreStatusListener;
import com.shenhaofeng.app.loader.list.component.SwipeRecyclerComponent;
import com.shenhaofeng.app.loader.list.listener.OnBindDataListener;
import com.shenhaofeng.app.loader.list.listener.OnRefreshStatusListener;
import com.shenhaofeng.app.loader.list.listener.impl.OnRefreshStatusListenerImpl;
import com.shenhaofeng.demo.R;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import rx.Observable;


/**
 * Created by Administrator on 2016/9/30.
 */
public class ListActivity extends BaseActivity implements DataSource<String>, OnBindDataListener<String>, OnMoreStatusListener<String> {


    private SingleTypeMoreAdapter<? extends RecyclerView.ViewHolder, String> adapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SwipeRefreshLayout swipeRefreshLayout = new SwipeRefreshLayout(this);
        RecyclerView recyclerView = new RecyclerView(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        swipeRefreshLayout.addView(recyclerView);
        setContentView(swipeRefreshLayout);
        adapter = new SingleTypeMoreAdapter<RecyclerView.ViewHolder, String>() {
            @Override
            protected void onBindSingleItemViewHolder(RecyclerView.ViewHolder holder, int position) {
                VH vh = (VH) holder;
                vh.title.setText(String.valueOf(position));
            }


            @Override
            protected RecyclerView.ViewHolder onCreateSingleViewHolder(ViewGroup parent, int viewType) {
                return new VH(LayoutInflater.from(parent.getContext()).inflate(R.layout.act_main_item, parent, false));
            }
        };
        recyclerView.setAdapter(adapter);
        Loader<SwipeRefreshLayout, RecyclerView, String> loader =
                new Loader<>(new SwipeRecyclerComponent<>(new OnRefreshStatusListenerImpl<String>() {
                    @Override
                    public void onRefreshCompleted(SwipeRefreshLayout swipeRefreshLayout) {
                        super.onRefreshCompleted(swipeRefreshLayout);
                        adapter.uninstallStatus();
                    }
                }, adapter, this));
        loader.attach(swipeRefreshLayout, recyclerView, this, this);
        loader.refresh();
    }


    @Override
    public Observable<String> onRequestData(int page, int pageSize) {

        System.out.println("request_page=" + page);
        return Observable.just("test").delay(1, TimeUnit.SECONDS);
    }


    @Override
    public void onBindDataToAdapter(View adapterView, int page, String data) {
        System.out.println("page=" + page);
        if (page == 1) {
            adapter.clearAllContentItem();
        }
        List<String> da = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            da.add(data);
        }
        adapter.getModule().addItems(da);

    }


    @Override
    public void onLoadStart(RecyclerView recyclerView, RecyclerView.Adapter adapter) {
        this.adapter.installStatus();
        this.adapter.getStatus().setCurrentStatus(Status.STATUS_MORE_LOADING);
    }


    @Override
    public void onLoadSuccess(RecyclerView recyclerView, RecyclerView.Adapter adapter, String data) {

    }


    @Override
    public void onLoadCompleted(RecyclerView recyclerView, RecyclerView.Adapter adapter) {

    }


    @Override
    public void onLoadThrowable(RecyclerView recyclerView, RecyclerView.Adapter adapter, Throwable throwable) {

    }


    class VH extends RecyclerView.ViewHolder {
        public TextView title;


        public VH(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
        }
    }
}
